package com.cloudeggtech.granite.chat.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cloudeggtech.granite.chat.R;
import com.cloudeggtech.granite.chat.helper.Util;
import com.cloudeggtech.granite.chat.model.UserInfo;
import com.cloudeggtech.granite.sdk.GIMCallback;
import com.cloudeggtech.granite.sdk.GIMManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by david on 17/10/26.
 */

public class LoginActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "LoginActivity";
    private EditText mobile;
    private EditText password;
    private Button btnLogin;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mobile = (EditText) findViewById(R.id.phone);
        password = (EditText) findViewById(R.id.password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        mobile.setText("agilest");
        password.setText("a_good_guy");
        Log.d("LoginActivity", "on login");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public void success() {
        Log.d(TAG, "login success");
        Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);
        finish();
    }


    public void failure(Throwable t) {
        Log.e(TAG, "login failed:", t);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String m = mobile.getText().toString(), p = password.getText().toString();
                if (m.length() == 0 || p.length() == 0) {
                    Util.showToast(this, getResources().getString(R.string.empty_mobile_password));
                    break;
                }
//                UserInfo user = new UserInfo(m, p);
//                user.login();

                GIMManager.getInstance().login(m, p, new GIMCallback() {
                    @Override
                    public void success() {
                        LoginActivity.this.success();
                    }

                    @Override
                    public void failure(Throwable t) {
                        LoginActivity.this.failure(t);
                    }
                });
                break;
        }
    }
}
